package com.stefanini.resultmundial2014.controladores;

import com.fifa.filters.SecurityFilter;
import com.stefanini.mundial2014capadatos.Usuario;
import com.stefanini.resultmundial2014.ejb.UserManager;
import java.io.Serializable;
import java.util.Locale;
//import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
//import javax.ejb.Local;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

/**
 *
 * @author CAMALEON
 */

@Named(value ="loginController")
@RequestScoped
public class LoginController implements Serializable{
   private static final long serialVersionUID = 1L; 
    
   /*Variables*/
   private String usuario;
   private String password; 
   
   @EJB
   private UserManager userManager;
   
   /*Metodo que verifica si existe el usuario*/
   public void login (){
       Usuario user = userManager.verificarLogin(getUsuario(), getPassword());
       FacesContext ctx = FacesContext.getCurrentInstance();
       
       if (user == null){
           ctx.addMessage(null,
                   new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error",
                   "Usuario o Contraseña invalida"));
       }else {
       HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
       session.setAttribute (SecurityFilter.LOGGED_USER,user.getUsername());
       session.setAttribute (SecurityFilter.IS_LOGGED_IN,Boolean.TRUE);
       
       try {
           //String url = ResourceBundle.getBundle("com.fifa.urls").getString("index.url");
           //ctx.getExternalContext().redirect(url);
           // Otra forma de hacerlo
           ctx.getExternalContext().redirect("/ResultMundial2014/pages/index.xhtml");
       } catch (Exception e) {
           Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE,"Error al redireccionar",e);
            }
       }
   }
   
   /*Metodo que quita o desliga la sesion abierta*/
   public void logout (){
       FacesContext ctx = FacesContext.getCurrentInstance();
       HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
       session.invalidate();
       try {
           ctx.getExternalContext().redirect("/ResultMundial2014/pages/index.xhtml");
       } catch (Exception ex) {
           Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE,"Error al redireccionar",ex);
       }
   }
   
   /****************** Internacionalización ******************/
   /*Metodo para cambiar el idioma de los textos en lapágina*/
   public void changeLanguaje(){
       if (FacesContext.getCurrentInstance()
               .getViewRoot().getLocale().equals(new Locale("es"))){
           FacesContext.getCurrentInstance()
                   .getViewRoot().setLocale(Locale.ENGLISH);
       }else{
           FacesContext.getCurrentInstance()
                   .getViewRoot().setLocale(new Locale("es"));
       }
   }
   
    /****** METODOS ACCESORES ******/
   /*Usuario*/
    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /*Password*/
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    } 
}